﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace drawstraws
{
    public partial class mainForm : Form
    {
        public string[] nameList;
        List<Person> persons = new List<Person>();
        Random random = new Random();
        public mainForm()
        {
            InitializeComponent();
        }

        private void btLoad_Click(object sender, EventArgs e)
        {
            loadForm loadform = new loadForm();
            
            loadform.Owner = this;
            loadform.ShowDialog();
            //如果没有，就退出
            if (loadform.nameList == null)
                return;
            nameList = loadform.nameList;
            int amount=nameList.Count();
            //原来的清空
            persons.Clear();
            for (int i = 0; i < amount; i++)
            {
                //去掉前后空格
                string name = nameList[i].Trim();
                if ( name!= "")
                {
                    int rank = random.Next(1000);
                    Person p=new Person(name,rank);
                    persons.Add(p);

                }
            }
            int pCount=persons.Count;
            dataGridView.Rows.Clear();
            for (int i = 0; i < pCount; i++)
            {
                dataGridView.Rows.Add(i+1,persons[i].Name,persons[i].Rank);
            }
            MessageBox.Show("载入成功，共"+pCount+"人。", "成功", MessageBoxButtons.OK);    
        }

        private void btDraw_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult;
            int pCount = persons.Count;
            if (pCount <= 0)
            {
                MessageBox.Show("请先载入名单！", "提示", MessageBoxButtons.OK);
                return;
            }

            dialogResult = MessageBox.Show("确定要开始抽签么？", "抽签", MessageBoxButtons.OKCancel);
            if(dialogResult==DialogResult.Cancel){
                 return;
            }

            //随机赋值，然后排序
            for (int i = 0; i < pCount; i++)
            {
                persons[i].Rank = random.Next(1000);
            }
            persons.Sort();
            dataGridView.Rows.Clear();
            for (int i = 0; i < pCount; i++)
            {
                dataGridView.Rows.Add(i + 1, persons[i].Name, persons[i].Rank);
            }
        }
    }
}
